<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_rooms extends MY_Model {
	
	/* 	
		-------------------------------------------------------------------
		add_room
		12-11-12
		perry
		-------------------------------------------------------------------
		@param $room_data - array containing the room data
		
		Adds a room
		
		Returns true if add success
		Returns false if add unsuccessful
	*/
	function add_room($room_data)
	{
		$room_data['year_id'] = $this->c_sy->id;
		$this->db->insert('rooms',$room_data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}	
	
	/* 	
		-------------------------------------------------------------------
		edit_room
		12-11-12
		perry
		-------------------------------------------------------------------
		@param $room_data - array containing the room data
		@param $room_id - id of the room being edited
		
		Updates a room
		
		Returns true if update success
		Returns false if update unsuccessful
	*/
	function edit_room($room_data, $room_id)
	{
		$this->db->where('room_id', $room_id)->update('rooms', $room_data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	
	/* 	
		-------------------------------------------------------------------
		delete_room
		12-11-12
		perry
		-------------------------------------------------------------------
		@param $room_id - id of the room to be deleted
		
		Deletes a room
		
		Returns true if delete success
		Returns false if delete unsuccessful
	*/
	function delete_room($room_id){
		$this->db->where('room_id', $room_id)->delete('rooms');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	/* 	
		-------------------------------------------------------------------
		get_room
		12-11-12
		perry
		-------------------------------------------------------------------
		@param $room_id - id of the room
		
		Gets information about a room
		
		Returns true if a room is found
		Returns false if a room is not found
	*/
	function get_room($room_id){
		$q = $this->db->select('room_id, rooms.level_id as level_id, max_students, name, description, level_code, level_desc')
			->from('rooms')
			->join('levels', 'rooms.level_id = levels.level_id')
			->where('room_id', $room_id)
			->where('year_id',$this->c_sy->id)
			->get();
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	/* 	
		-------------------------------------------------------------------
		get_all_rooms
		12-11-12
		perry
		-------------------------------------------------------------------
		Gets all rooms
		
		Returns all rooms if rooms are present
		Returns false if no rooms are present
	*/
	function get_all_rooms(){
		$q = $this->db
			->select('room_id, levels.level_desc,rooms.level_id as level_id,section_id ,max_students, name, description, level_code, level_desc')
			->from('rooms')
			->join('levels', 'rooms.level_id = levels.level_id')
			->where('year_id',$this->c_sy->id)
			->get();
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	/* 	
		-------------------------------------------------------------------
		get_rooms_for_level
		12-12-12
		perry
		-------------------------------------------------------------------
		@param $level_id - the level_id of a grade level which we want to have.
		
		Gets a room for a particular grade level
		
		Returns true if rooms for a level is found
		Returns false if rooms for a level is not found
	*/
	function get_rooms_for_level($level_id){
		$q = $this->db
			->select('room_id, rooms.level_id as level_id, name, description, level_code, level_desc')
			->from('rooms')
			->join('levels', 'rooms.level_id = levels.level_id')
			->where('rooms.level_id', $level_id)
			->where('year_id',$this->c_sy->id)
			->get();
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	function get_room_section_level($id = FALSE,$teacher_id = FALSE)
	{
		$this->db->select('r.section_id,
						   r.room_id,
						   r.name,
						   r.description,
						   s.max_load as max_students,
						   s.section_name,
						   l.level_id,
						   l.level_desc')
		         ->from('rooms r');
				 
		if($id !== FALSE)
		{
			$this->db->where('s.grade_level_id',$id);
		}
		if($teacher_id !== FALSE)
		{
			$this->db->where('s.adviser_id',$teacher_id);
		}
		
		$this->db->where('r.year_id',$this->c_sy->id)
				 ->join('sections s','r.section_id = s.id','LEFT')
				 ->join('levels l','l.level_id = s.grade_level_id','LEFT')
				 ->order_by('s.grade_level_id','ASC');
		$q = $this->db->get();
		
		// pd($this->db->last_query());
		return $q->result();
	}
	
	public function get_room_section($id)
	{
		$sql = "SELECT r.room_id,
					   s.section_name,
					   s.id as section_id,
					   r.name as room_name,
					   r.description as room_desc,
					   concat(emp.first_name,' ',emp.middle_name,' ',emp.last_name) as adviser_name
				FROM rooms r
				LEFT JOIN sections s ON r.section_id = s.id
				LEFT JOIN employees emp ON s.adviser_id = emp.emp_id
				WHERE r.room_id = ? 
				AND r.year_id = ?";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() > 0 ? $query->row() : FALSE;
	}
	
	public function get_room_section_students($id)
	{
		$sql = "SELECT CONCAT(prof.lastname, ', ', prof.firstname, ' ', prof.middlename) as students_name,e.e_id as enrollment_id
				FROM enrollments e
				LEFT JOIN profiles prof ON prof.profile_id = e.e_profile_id
				LEFT JOIN sections sec ON sec.id = e.e_section_id
				LEFT JOIN rooms r ON r.section_id = e.e_section_id
				LEFT JOIN schedule sch ON sch.section_id = e.e_section_id
				WHERE r.room_id = ?
				AND r.year_id = ?
				AND sec.year_id = ?
				GROUP BY e.e_profile_id
                ORDER BY prof.lastname
				";
		
		$query = $this->db->query($sql,array($id,$this->c_sy->id,$this->c_sy->id));
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function generate_rooms_for_current_year()
	{
		$get_sections = $this->db->where('year_id',$this->c_sy->id)->get('rooms');
		
		// pd($get_sections->result());
		if($get_sections->num_rows() >=1)
		{
			return TRUE;
		}else{
			$check_room = $this->db->group_by('name')->get('rooms');			
			if($check_room->num_rows() >= 1)
			{
				foreach($check_room->result() as $k => $v)
				{
					$new_rooms[$k]['name'] = $v->name;
					$new_rooms[$k]['description'] = $v->description;
					$new_rooms[$k]['year_id'] = $this->c_sy->id;
					$new_rooms[$k]['created'] = NOW;
	
				}
				
				
				$this->db->insert_batch('rooms',$new_rooms);
			}
			
			log_message('e','Unable to re create rooms');
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}
	}
}

?>